package com.bawei.hilt_test;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.ActivityComponent;
import dagger.hilt.android.scopes.ActivityScoped;
import dagger.hilt.components.SingletonComponent;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
//@Module:提供Retrofit的组件
//@InstallIn:装载到甚么地方 SingletonComponent.class和 @Singleton保证单例
@Module
@InstallIn(SingletonComponent.class)
public class RetrofitManager {
    //提供Retrofit的方法
    @Provides
    @Singleton
    @RetrofitStandard
    public Retrofit getRetrofit(){
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();
        Retrofit mRetrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl("http://43.143.146.165:8181")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        return mRetrofit;

    }
    //提供token.Retrofit的方法
    @Provides
    @Singleton
    @RetrofitToken
    public Retrofit getTokenRetrofit(){
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .addInterceptor(new TokenInterceptor())//token拦截器
                .build();
        Retrofit mRetrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl("http://43.143.146.165:8181")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        return mRetrofit;

    }
}
